package com.tianxiabuyi.weixin.common;

import com.tianxiabuyi.utils.HttpUtil;

/**
 * 微信令牌的获取及保持
 */
public abstract class AccessTokenHolder {


    private String clientId;
    private String clientSecret;
    private String tokenUrl;

    public AccessTokenHolder(String tokenUrl, String clientId, String clientSecret) {
        this.tokenUrl = tokenUrl;
        this.clientId = clientId;
        this.clientSecret = clientSecret;
    }


    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

    public void setTokenUrl(String tokenUrl) {
        this.tokenUrl = tokenUrl;
    }

    protected String fetchAccessToken() {
        String url = String.format(this.tokenUrl, this.clientId, this.clientSecret);
        String json =  HttpUtil.get(url);
        return json;
    }

    /**
     * 获取access token
     * @return
     */
    public abstract AccessToken getAccessToken();

    /**
     * 强制刷新
     */
    public abstract void refreshToken();

    /**
     * 强制设置token过期
     */
    public abstract void expireToken();
}
